//
//  XMLTabBarController.swift
//  Weibo
//
//  Created by mac on 16/5/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD


class XMLTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tabBar = XMLTabbar()
//        self.tabBar = tabBar//只读属性不能这样赋值
        setValue(tabBar, forKey: "tabBar")
        tabBar.xmlDelegate = self
        tabBar.composeButtonClosure = { [weak self] in
        
//            print("闭包")
//            print(self)
            if XMLUserAcountViewModel.sharedUserAcountViewModel.isLogin {
            
                self?.addComposeView()
                
            }else {
            
                SVProgressHUD.showErrorWithStatus("您还未登录，请先登录")
            }
        }

        addChildViewController(XMLHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(XMLMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(XMLDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(XMLProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
    }
    
    private func addComposeView() {
    
        let composeVC = XMLComposeViewController()
        let nav = UINavigationController(rootViewController: composeVC)
        
        presentViewController(nav, animated: true, completion: nil)
    }
    
    func addChildViewController(childController: UIViewController, title :String, imageName :String) {
        //设置全局统一的tabbar的选中颜色，这句代码执行的越早越好，可以考虑放到Appdelegate中去
//        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        childController.title = title
        
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(15)], forState: .Normal)
        
        let nav = UINavigationController(rootViewController: childController)
        
        addChildViewController(nav)
    }

}

extension XMLTabBarController :XMLTabbarDelegate {
    
    func composeButtonClick() {
//        print("代理")
    }
}




